﻿using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Unit;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups
{
    public partial class PortSenserInfo : UIForm
    {
        public string PortID;
        LifterSEM lftSem = (LifterSEM) Naming.GetDeviceManager().GetDevice();
        public PortSenserInfo()
        {
            InitializeComponent();
        }

        public void Subscribe()
        {
            LifterPort lifterPort = this.lftSem.GetLifterPort(this.PortID);
            if (lifterPort != null)
            {
                lifterPort.pIODelegate += UpadatePIOInfo;
                lifterPort.senserInfoDelegate += UpadateSenserInfo;
            }
        }
        public void UpadatePIOInfo(int value)
        {
            if (value < 0)
            {
                value = 65536 + value;
            }
            //将10进制转换成2进制char数组
            char[] pioArray = Convert.ToString(value, 2).ToCharArray();
            char[] comChar;
            char[] char0 = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
            Array.Reverse(pioArray);
            //如果转换的2进制数组不满16位，则需要按位补充到16位
            if (pioArray.Length < 16)
            {
                comChar = pioArray.Concat(char0.Take(16 - pioArray.Length)).ToArray();
            }
            else
            {
                comChar = pioArray;
            }
            foreach (Control control in this.portPIOInfoPanel.Controls)
            {
                if (control is UILight)
                {
                    UILight pioSenser = (UILight)control;
                    if ("PIO1".Equals(pioSenser.Name))
                    {
                        if (comChar[0].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO2".Equals(pioSenser.Name))
                    {
                        if (comChar[1].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO3".Equals(pioSenser.Name))
                    {
                        if (comChar[2].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO4".Equals(pioSenser.Name))
                    {
                        if (comChar[3].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO5".Equals(pioSenser.Name))
                    {
                        if (comChar[4].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO6".Equals(pioSenser.Name))
                    {
                        if (comChar[5].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO7".Equals(pioSenser.Name))
                    {
                        if (comChar[6].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO8".Equals(pioSenser.Name))
                    {
                        if (comChar[7].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO9".Equals(pioSenser.Name))
                    {
                        if (comChar[8].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO10".Equals(pioSenser.Name))
                    {
                        if (comChar[9].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO11".Equals(pioSenser.Name))
                    {
                        if (comChar[10].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO12".Equals(pioSenser.Name))
                    {
                        if (comChar[11].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO13".Equals(pioSenser.Name))
                    {
                        if (comChar[12].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO14".Equals(pioSenser.Name))
                    {
                        if (comChar[13].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO15".Equals(pioSenser.Name))
                    {
                        if (comChar[14].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("PIO16".Equals(pioSenser.Name))
                    {
                        if (comChar[15].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }

                }
            }
        }
        public void UpadateSenserInfo(int value)
        {
            if (value < 0)
            {
                value = 65536 + value;
            }
            //将10进制转换成2进制char数组
            char[] pioArray = Convert.ToString(value, 2).ToCharArray();
            char[] comChar;
            char[] char0 = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
            Array.Reverse(pioArray);
            //如果转换的2进制数组不满16位，则需要按位补充到16位
            if (pioArray.Length < 16)
            {
                comChar = pioArray.Concat(char0.Take(16 - pioArray.Length)).ToArray();
            }
            else
            {
                comChar = pioArray;
            }

            foreach (Control control in this.SensorInfoPanel.Controls)
            {
                if (control is UILight)
                {
                    UILight pioSenser = (UILight)control;
                    if ("PresenceSensor".Equals(pioSenser.Name))
                    {
                        if (comChar[0].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("DecelerateSensor".Equals(pioSenser.Name))
                    {
                        if (comChar[1].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("StopSensor".Equals(pioSenser.Name))
                    {
                        if (comChar[2].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                    else if ("SafetyGratingSensor".Equals(pioSenser.Name))
                    {
                        if (comChar[3].Equals('0'))
                        {
                            pioSenser.State = UILightState.Off;
                        }
                        else
                        {
                            pioSenser.State = UILightState.On;
                        }
                    }
                }
            }
        }
        private void PortSenserInfo_FormClosed(object sender, FormClosedEventArgs e)
        {
            LifterPort lifterPort = this.lftSem.GetLifterPort(this.PortID);
            lifterPort.pIODelegate -= UpadatePIOInfo;
        }
    }
}
